<template>
  <div class="app-container">
    <!--条件搜索表单-->
    <el-row>
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="searchObj.userName" placeholder="用户名称" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="searchObj.loginAddr" placeholder="登录地址" />
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchObj.loginState" placeholder="登录状态" @change="getAll">
            <el-option value="0" label="失败" />
            <el-option value="1" label="成功" />
          </el-select>
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getAll()">查询</el-button>
        <el-button type="default" icon="el-icon-refresh" @click="reset()">清空</el-button>
      </el-form>
    </el-row>

    <el-row>
      <el-button type="danger" icon="el-icon-delete" @click="batchDelete">批量删除</el-button>
      <el-button type="primary" icon="el-icon-download" @click="exportData">批量导出</el-button>
    </el-row>

    <el-row>
      <!-- banner列表 :data="list"表示遍历list数组 stripe条纹 -->
      <el-table
        :data="pageList.list"
        :border="false"
        stripe
        v-loading="loading"
        element-loading-text="正在努力加载数据中..."
        style="width: 100%; margin: 15px 0px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" fixed="left" width="55" />
        <el-table-column type="index" fixed="left" width="80" label="序号" />
        <el-table-column prop="userName" fixed="left" label="用户名称" show-overflow-tooltip />
        <el-table-column prop="loginIp" label="登录IP" show-overflow-tooltip />
        <el-table-column prop="loginAddr" label="登录地址" show-overflow-tooltip />
        <el-table-column prop="driverName" label="浏览器" show-overflow-tooltip />
        <el-table-column prop="osName" label="操作系统" show-overflow-tooltip />
        <el-table-column prop="loginState" label="登录状态" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.loginState === 1" type="success">成功</el-tag>
            <el-tag v-if="scope.row.loginState === 0" type="danger">失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="loginTime" label="登录时间" show-overflow-tooltip />
      </el-table>
    </el-row>

    <!-- 分页导航栏 -->
    <el-row>
      <el-pagination
        background
        :current-page="pIndex"
        :page-size="pSize"
        :total="pageList.total"
        :page-sizes="[3, 6, 9, 12, 15, 20]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        @current-change="getAll"
        @size-change="handleSizeChange"
      />
    </el-row>
  </div>
</template>

<script>
// 引入api接口定义的js文件
import logApi from '@/api/sys/loginLog'

export default {
  data() {
    // 定义变量和初始值
    return {
      pIndex: 1, // 当前页
      pSize: 6, // 每页显示记录数
      searchObj: {}, // 条件封装对象
      ids: [],
      pageList: {},
      loading: false
    }
  },
  created() {
    // 勾子函数，在页面渲染之前执行，一般调用methods定义的方法得到数据
    this.getAll() // 调用methods中的函数
  },
  methods: {
    // 用户信息
    getAll(current = 1) {
      this.loading = true
      // 加载列表数据
      this.pIndex = current // 添加当前页参数
      // axios异步获取远程数据（ajax），通过api
      logApi
        .getPage(this.pIndex, this.pSize, this.searchObj)
        .then((response) => {
          this.pageList = response.data
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },

    // 改变页码
    handleSizeChange(pageSize) {
      this.pSize = pageSize
      // 重新调用数据加载方法 --> 方法中已经加入的查询参数
      this.getAll()
    },

    // 复选框选中
    handleSelectionChange(r) {
      // 得到选中的id
      this.ids = r.map((v) => v.id)
    },

    // 批量删除
    batchDelete() {
      this.$confirm('此操作将永久删除登录日志信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确定执行then方法
        // 调用接口
        logApi.batchRemove(this.ids).then((resp) => {
          // 提示
          this.$message.success('删除成功!')
          // 刷新页面
          this.getAll()
        })
      })
    },

    // 导出
    exportData() {
      // 导出
      logApi.exportData().then((res) => {
        this.$message.success(res.data)
      })
    },

    // 重置
    reset() {
      // 清空
      this.searchObj = {}
      this.getAll()
    }
  }
}
</script>

<style></style>
